<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>文档数据库 MongoDB_03SpringBoot整合MongoDB | Oct25-X</title><meta name="keywords" content="MongoDB"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="SpringBoot 整合 MongoDB：导包及配置一、导入相关依赖1234&lt;dependency&gt;    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;    &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;&#x2F;artifactId&gt;&lt;&#x2F;depend">
<meta property="og:type" content="article">
<meta property="og:title" content="文档数据库 MongoDB_03SpringBoot整合MongoDB">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2023/09/18/25%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_03SpringBoot%E6%95%B4%E5%90%88MongoDB/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="SpringBoot 整合 MongoDB：导包及配置一、导入相关依赖1234&lt;dependency&gt;    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;    &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;&#x2F;artifactId&gt;&lt;&#x2F;depend">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt25.png">
<meta property="article:published_time" content="2023-09-18T02:10:16.000Z">
<meta property="article:modified_time" content="2024-02-17T14:27:32.490Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="MongoDB">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt25.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/09/18/25%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_03SpringBoot%E6%95%B4%E5%90%88MongoDB/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '文档数据库 MongoDB_03SpringBoot整合MongoDB',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-17 22:27:32'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg24.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">文档数据库 MongoDB_03SpringBoot整合MongoDB</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-09-18T02:10:16.000Z" title="发表于 2023-09-18 10:10:16">2023-09-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-17T14:27:32.490Z" title="更新于 2024-02-17 22:27:32">2024-02-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/NoSQL%E6%95%B0%E6%8D%AE%E5%BA%93/">NoSQL数据库</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="文档数据库 MongoDB_03SpringBoot整合MongoDB"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="SpringBoot-整合-MongoDB：导包及配置"><a href="#SpringBoot-整合-MongoDB：导包及配置" class="headerlink" title="SpringBoot 整合 MongoDB：导包及配置"></a>SpringBoot 整合 MongoDB：导包及配置</h1><h2 id="一、导入相关依赖"><a href="#一、导入相关依赖" class="headerlink" title="一、导入相关依赖"></a>一、导入相关依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-mongodb<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="二、配置文件"><a href="#二、配置文件" class="headerlink" title="二、配置文件"></a>二、配置文件</h2><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">port</span>: <span class="string">8081</span></span><br><span class="line"></span><br><span class="line"><span class="attr">spring</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">application</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">name</span>: <span class="string">mongorepository</span></span><br><span class="line">  <span class="attr">data</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">mongodb</span>:<span class="string"></span></span><br><span class="line"><span class="comment">      #本地</span></span><br><span class="line">      <span class="attr">uri</span>: <span class="string">mongodb://127.0.0.1:27017/test</span></span><br></pre></td></tr></table></figure>

<p>连接其他情况：</p>
<ul>
<li>本机无验证</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">data</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">mongodb</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">uri</span>: <span class="string">mongodb://127.0.0.1:27017/test</span></span><br></pre></td></tr></table></figure>

<ul>
<li>本机需要验证</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">data</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">mongodb</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">uri</span>: <span class="string">mongodb://root:123456@127.0.0.1:27017/test</span></span><br></pre></td></tr></table></figure>

<ul>
<li>连接副本集群</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">data</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">mongodb</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">uri</span>: <span class="string">mongodb://192.168.200.200:29017,192.168.200.200:29018,192.168.200.200:29019/test?replicaSet=replica-set-cluster</span></span><br></pre></td></tr></table></figure>

<ul>
<li>连接分片集群</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">data</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">mongodb</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">uri</span>: <span class="string">mongodb://192.168.200.200:29017,192.168.200.200:29018,192.168.200.200:29019/test?replicaSet=replica-set-cluster</span></span><br></pre></td></tr></table></figure>

<h2 id="三、创建对应实体类"><a href="#三、创建对应实体类" class="headerlink" title="三、创建对应实体类"></a>三、创建对应实体类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.mongotemplate.domain;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> org.bson.types.ObjectId;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.mapping.Document;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.mapping.Field;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.mapping.MongoId;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2022年10月24</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@Document(collection = &quot;book&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Book</span> &#123;</span><br><span class="line">    <span class="meta">@MongoId</span></span><br><span class="line">    <span class="keyword">private</span> ObjectId id;</span><br><span class="line">    <span class="meta">@Field(&quot;name&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="meta">@Field(&quot;author&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String author;</span><br><span class="line">    <span class="meta">@Field(&quot;label&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String label;</span><br><span class="line">    <span class="meta">@Field(&quot;score&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> Double score;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注解说明：</p>
<ul>
<li>@Document：标注当前实体类对应集合</li>
<li>@MongoId：标注当前属性为 MongoDB 的id，需要注意的是，类型为 ObjectId 才可以识别为 MongoDB 中的 “_id”，并与其对应</li>
<li>@Field：标注 MongoDB 的字段</li>
</ul>
<h1 id="SpringBoot-整合-MongoDB：MongoRepository"><a href="#SpringBoot-整合-MongoDB：MongoRepository" class="headerlink" title="SpringBoot 整合 MongoDB：MongoRepository"></a>SpringBoot 整合 MongoDB：MongoRepository</h1><h2 id="一、数据访问接口"><a href="#一、数据访问接口" class="headerlink" title="一、数据访问接口"></a>一、数据访问接口</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.mongotemplate.repository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.example.mongotemplate.domain.Book;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.repository.MongoRepository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2022年10月24</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">BookRepository</span> <span class="keyword">extends</span> <span class="title class_">MongoRepository</span>&lt;Book, String&gt; &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * SQL: where label like ?1 and score &gt; ?1</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    List&lt;Book&gt; <span class="title function_">findBooksByNameLikeAndScoreGreaterThan</span><span class="params">(String name, Double score)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * SQL: where name not like ?1 order by score desc</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    List&lt;Book&gt; <span class="title function_">findBooksByNameNotLikeOrderByScoreDesc</span><span class="params">(String name)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注：Spring Data 提供了对 MongoDB 数据库访问的支持，我们只需要继承 MongoRepository，然后按照定义好的规范开发即可，Spring Data 方法定义规范如下：</p>
<p><img src="/oct25-xxxxx/img/mongodb/mongodb14.png"></p>
<p><img src="/oct25-xxxxx/img/mongodb/mongodb15.png"></p>
<h2 id="二、测试类测试-MongoDB-基本的增删改查"><a href="#二、测试类测试-MongoDB-基本的增删改查" class="headerlink" title="二、测试类测试 MongoDB 基本的增删改查"></a>二、测试类测试 MongoDB 基本的增删改查</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.mongorepository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.example.mongorepository.domain.Book;</span><br><span class="line"><span class="keyword">import</span> com.example.mongorepository.repository.BookRepository;</span><br><span class="line"><span class="keyword">import</span> org.bson.types.ObjectId;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MongorepositoryApplicationTests</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> BookRepository bookRepository;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 增</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 插入</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insert</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//        Book insert = bookRepository.insert(new Book(null, &quot;骆驼祥子&quot;, &quot;老舍&quot;, &quot;小说&quot;, 6.9));</span></span><br><span class="line"><span class="comment">//        Book insert = bookRepository.insert(new Book(null, &quot;红高粱&quot;, &quot;莫言&quot;, &quot;小说&quot;, 6.6));</span></span><br><span class="line">        <span class="type">Book</span> <span class="variable">insert</span> <span class="operator">=</span> bookRepository.insert(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">6.6</span>));</span><br><span class="line">        System.out.println(insert);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 批量删除</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insertBatch</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = Arrays.asList(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;唐诗三百首&quot;</span>, <span class="string">&quot;孙洙&quot;</span>, <span class="string">&quot;文学&quot;</span>, <span class="number">9.2</span>),</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">6.6</span>));</span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; insert = bookRepository.insert(books);</span><br><span class="line">        System.out.println(insert);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 删</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id删除</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">()</span> &#123;</span><br><span class="line">        bookRepository.deleteById(<span class="string">&quot;63565836aabcb935b10036ef&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 改</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id更新</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">        bookRepository.save(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="keyword">new</span> <span class="title class_">ObjectId</span>(<span class="string">&quot;63565ab78c9b13110185acfc&quot;</span>), <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">8.8</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 条件更新</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">updateByCondition</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 条件查询</span></span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findById(<span class="string">&quot;635655b2e612d507ef3d2f07&quot;</span>);</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> optional.get();</span><br><span class="line">        <span class="comment">// 修改</span></span><br><span class="line">        book.setScore(<span class="number">9.9</span>);</span><br><span class="line">        <span class="comment">// 带id的保存（save）</span></span><br><span class="line">        <span class="type">Book</span> <span class="variable">save</span> <span class="operator">=</span> bookRepository.save(book);</span><br><span class="line">        System.out.println(save);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 查</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAll</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; all = bookRepository.findAll();</span><br><span class="line">        System.out.println(all);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findById</span><span class="params">()</span> &#123;</span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findById(<span class="string">&quot;634f9754e5211a4bcbbd1118&quot;</span>);</span><br><span class="line">        System.out.println(optional);</span><br><span class="line">        System.out.println(optional.get());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据条件查询一条文档</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findOne</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">        book.setName(<span class="string">&quot;三国演义&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findOne(Example.of(book));</span><br><span class="line">        System.out.println(optional);</span><br><span class="line">        System.out.println(optional.get());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 模糊/匹配查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAllByMatch</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 定义查询规则</span></span><br><span class="line">        <span class="comment">// CONTAINING：包含</span></span><br><span class="line">        <span class="comment">// STARTING：开始</span></span><br><span class="line">        <span class="comment">// ENDING：结尾</span></span><br><span class="line">        <span class="comment">// REGEX：正则</span></span><br><span class="line">        <span class="type">ExampleMatcher</span> <span class="variable">matcher</span> <span class="operator">=</span> ExampleMatcher.matching()</span><br><span class="line">                .withStringMatcher(ExampleMatcher.StringMatcher.REGEX)</span><br><span class="line">                .withIgnoreCase(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line"><span class="comment">//        book.setName(&quot;三&quot;); // CONTAINING、STARTING</span></span><br><span class="line"><span class="comment">//        book.setName(&quot;人&quot;); // ENDING</span></span><br><span class="line">        book.setName(<span class="string">&quot;^三.*$&quot;</span>); <span class="comment">// REGEX</span></span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findAll(Example.of(book, matcher));</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 分页查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAllByPage</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// pageNum=0, pageSize=3</span></span><br><span class="line">        <span class="type">Pageable</span> <span class="variable">pageable</span> <span class="operator">=</span> PageRequest.of(<span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">ExampleMatcher</span> <span class="variable">matcher</span> <span class="operator">=</span> ExampleMatcher.matching()</span><br><span class="line">                .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)</span><br><span class="line">                .withIgnoreCase(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">        book.setName(<span class="string">&quot;三&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Page&lt;Book&gt; page = bookRepository.findAll(Example.of(book, matcher), pageable);</span><br><span class="line">        System.out.println(<span class="string">&quot;总数据：&quot;</span> + page.getTotalElements());</span><br><span class="line">        System.out.println(<span class="string">&quot;总页数：&quot;</span> + page.getTotalPages());</span><br><span class="line">        System.out.println(<span class="string">&quot;页长度：&quot;</span> + page.getSize());</span><br><span class="line">        System.out.println(<span class="string">&quot;数据：&quot;</span> + page.getContent());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 其他</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByNameAndScore</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findBooksByNameLikeAndScoreGreaterThan(<span class="string">&quot;三&quot;</span>, <span class="number">9.0</span>);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByNameAndOrderByScore</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findBooksByNameNotLikeOrderByScoreDesc(<span class="string">&quot;三&quot;</span>);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">package</span> com.example.mongorepository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.example.mongorepository.domain.Book;</span><br><span class="line"><span class="keyword">import</span> com.example.mongorepository.repository.BookRepository;</span><br><span class="line"><span class="keyword">import</span> org.bson.types.ObjectId;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MongorepositoryApplicationTests</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> BookRepository bookRepository;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 增</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 插入</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insert</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//        Book insert = bookRepository.insert(new Book(null, &quot;骆驼祥子&quot;, &quot;老舍&quot;, &quot;小说&quot;, 6.9));</span></span><br><span class="line"><span class="comment">//        Book insert = bookRepository.insert(new Book(null, &quot;红高粱&quot;, &quot;莫言&quot;, &quot;小说&quot;, 6.6));</span></span><br><span class="line">        <span class="type">Book</span> <span class="variable">insert</span> <span class="operator">=</span> bookRepository.insert(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">6.6</span>));</span><br><span class="line">        System.out.println(insert);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 批量删除</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insertBatch</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = Arrays.asList(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;唐诗三百首&quot;</span>, <span class="string">&quot;孙洙&quot;</span>, <span class="string">&quot;文学&quot;</span>, <span class="number">9.2</span>),</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">6.6</span>));</span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; insert = bookRepository.insert(books);</span><br><span class="line">        System.out.println(insert);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 删</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id删除</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">()</span> &#123;</span><br><span class="line">        bookRepository.deleteById(<span class="string">&quot;63565836aabcb935b10036ef&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 改</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id更新</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">        bookRepository.save(<span class="keyword">new</span> <span class="title class_">Book</span>(<span class="keyword">new</span> <span class="title class_">ObjectId</span>(<span class="string">&quot;63565ab78c9b13110185acfc&quot;</span>), <span class="string">&quot;人生&quot;</span>, <span class="string">&quot;路遥&quot;</span>, <span class="string">&quot;小说&quot;</span>, <span class="number">8.8</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 条件更新</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">updateByCondition</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 条件查询</span></span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findById(<span class="string">&quot;635655b2e612d507ef3d2f07&quot;</span>);</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> optional.get();</span><br><span class="line">        <span class="comment">// 修改</span></span><br><span class="line">        book.setScore(<span class="number">9.9</span>);</span><br><span class="line">        <span class="comment">// 带id的保存（save）</span></span><br><span class="line">        <span class="type">Book</span> <span class="variable">save</span> <span class="operator">=</span> bookRepository.save(book);</span><br><span class="line">        System.out.println(save);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 查</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAll</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; all = bookRepository.findAll();</span><br><span class="line">        System.out.println(all);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据id查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findById</span><span class="params">()</span> &#123;</span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findById(<span class="string">&quot;634f9754e5211a4bcbbd1118&quot;</span>);</span><br><span class="line">        System.out.println(optional);</span><br><span class="line">        System.out.println(optional.get());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 根据条件查询一条文档</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findOne</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">        book.setName(<span class="string">&quot;三国演义&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Optional&lt;Book&gt; optional = bookRepository.findOne(Example.of(book));</span><br><span class="line">        System.out.println(optional);</span><br><span class="line">        System.out.println(optional.get());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 模糊/匹配查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAllByMatch</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 定义查询规则</span></span><br><span class="line">        <span class="comment">// CONTAINING：包含</span></span><br><span class="line">        <span class="comment">// STARTING：开始</span></span><br><span class="line">        <span class="comment">// ENDING：结尾</span></span><br><span class="line">        <span class="comment">// REGEX：正则</span></span><br><span class="line">        <span class="type">ExampleMatcher</span> <span class="variable">matcher</span> <span class="operator">=</span> ExampleMatcher.matching()</span><br><span class="line">                .withStringMatcher(ExampleMatcher.StringMatcher.REGEX)</span><br><span class="line">                .withIgnoreCase(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line"><span class="comment">//        book.setName(&quot;三&quot;); // CONTAINING、STARTING</span></span><br><span class="line"><span class="comment">//        book.setName(&quot;人&quot;); // ENDING</span></span><br><span class="line">        book.setName(<span class="string">&quot;^三.*$&quot;</span>); <span class="comment">// REGEX</span></span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findAll(Example.of(book, matcher));</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 分页查询</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findAllByPage</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// pageNum=0, pageSize=3</span></span><br><span class="line">        <span class="type">Pageable</span> <span class="variable">pageable</span> <span class="operator">=</span> PageRequest.of(<span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">ExampleMatcher</span> <span class="variable">matcher</span> <span class="operator">=</span> ExampleMatcher.matching()</span><br><span class="line">                .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)</span><br><span class="line">                .withIgnoreCase(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">        book.setName(<span class="string">&quot;三&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Page&lt;Book&gt; page = bookRepository.findAll(Example.of(book, matcher), pageable);</span><br><span class="line">        System.out.println(<span class="string">&quot;总数据：&quot;</span> + page.getTotalElements());</span><br><span class="line">        System.out.println(<span class="string">&quot;总页数：&quot;</span> + page.getTotalPages());</span><br><span class="line">        System.out.println(<span class="string">&quot;页长度：&quot;</span> + page.getSize());</span><br><span class="line">        System.out.println(<span class="string">&quot;数据：&quot;</span> + page.getContent());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 其他</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByNameAndScore</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findBooksByNameLikeAndScoreGreaterThan(<span class="string">&quot;三&quot;</span>, <span class="number">9.0</span>);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByNameAndOrderByScore</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; books = bookRepository.findBooksByNameNotLikeOrderByScoreDesc(<span class="string">&quot;三&quot;</span>);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="SpringBoot-整合-MongoDB：MongoTemplate"><a href="#SpringBoot-整合-MongoDB：MongoTemplate" class="headerlink" title="SpringBoot 整合 MongoDB：MongoTemplate"></a>SpringBoot 整合 MongoDB：MongoTemplate</h1><h2 id="一、测试类测试-MongoDB-基本的增删改查"><a href="#一、测试类测试-MongoDB-基本的增删改查" class="headerlink" title="一、测试类测试 MongoDB 基本的增删改查"></a>一、测试类测试 MongoDB 基本的增删改查</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.mongotemplate;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.example.mongotemplate.domain.Book;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.result.DeleteResult;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.result.UpdateResult;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.MongoTemplate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.query.Criteria;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.query.Query;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.query.Update;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.regex.Pattern;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MongotemplateApplicationTests</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> MongoTemplate mongoTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 集合操作</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">collectionOpt</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 增</span></span><br><span class="line"><span class="comment">//        MongoCollection&lt;Document&gt; student = mongoTemplate.createCollection(&quot;student&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.println(student);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 删</span></span><br><span class="line">        mongoTemplate.dropCollection(<span class="string">&quot;student&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 文档操作</span></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 增</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//插入文档</span></span><br><span class="line">    <span class="comment">//db.book.insert(&#123;&quot;name&quot;:&quot;活着&quot;, &quot;author&quot;:&quot;余华&quot;, &quot;label&quot;:&quot;文学&quot;, &quot;score&quot;:&quot;8.3&quot;&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">createBook</span><span class="params">()</span> &#123;</span><br><span class="line"><span class="comment">//        Book book = new Book(null, &quot;活着&quot;, &quot;余华&quot;, &quot;文学&quot;, 8.3);</span></span><br><span class="line"><span class="comment">//        Book book = new Book(null, &quot;唐诗三百首&quot;, &quot;孙洙&quot;, &quot;文学&quot;, 9.2);</span></span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>(<span class="literal">null</span>, <span class="string">&quot;许三观卖血记&quot;</span>, <span class="string">&quot;余华&quot;</span>, <span class="string">&quot;文学&quot;</span>, <span class="number">7.2</span>);</span><br><span class="line">        <span class="type">Book</span> <span class="variable">insert</span> <span class="operator">=</span> mongoTemplate.insert(book);</span><br><span class="line">        System.out.println(insert);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 删</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//根据id删除文档</span></span><br><span class="line">    <span class="comment">//db.book.delete(&#123;&quot;_id&quot;:ObjectId(&quot;6350fb6955ddc14cd4c2b572&quot;)&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">deleteBook</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">DeleteResult</span> <span class="variable">deleteResult</span> <span class="operator">=</span> mongoTemplate.remove(<span class="keyword">new</span> <span class="title class_">Query</span>(Criteria.where(<span class="string">&quot;_id&quot;</span>).is(<span class="string">&quot;6350fb6955ddc14cd4c2b572&quot;</span>)), Book.class);</span><br><span class="line">        System.out.println(deleteResult);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 改</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//根据id更新文档</span></span><br><span class="line">    <span class="comment">//db.book.update(&#123;&quot;_id&quot;: ObjectId(&quot;634f9754e5211a4bcbbd1118&quot;)&#125;, &#123;$set: &#123;&quot;score&quot;: 9.9&#125;&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">updateBook</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 查询id</span></span><br><span class="line">        <span class="type">Query</span> <span class="variable">query</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Query</span>(Criteria.where(<span class="string">&quot;_id&quot;</span>).is(<span class="string">&quot;634f9754e5211a4bcbbd1118&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 根据id修改</span></span><br><span class="line">        <span class="type">Update</span> <span class="variable">update</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Update</span>();</span><br><span class="line">        update.set(<span class="string">&quot;score&quot;</span>, <span class="number">9.9</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">UpdateResult</span> <span class="variable">updateResult</span> <span class="operator">=</span> mongoTemplate.upsert(query, update, Book.class);</span><br><span class="line">        System.out.println(updateResult);</span><br><span class="line">        System.out.println(updateResult.getModifiedCount());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//更新多条记录</span></span><br><span class="line">    <span class="comment">//db.book.update(&#123;&quot;label&quot;: &quot;四大名著&quot;&#125;, &#123;$set: &#123;&quot;score&quot;: 10&#125;&#125;, &#123;multi : true&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">updateBookMulti</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">Update</span> <span class="variable">update</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Update</span>();</span><br><span class="line">        update.set(<span class="string">&quot;score&quot;</span>, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">UpdateResult</span> <span class="variable">updateResult</span> <span class="operator">=</span> mongoTemplate.updateMulti(</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Query</span>(Criteria.where(<span class="string">&quot;label&quot;</span>).is(<span class="string">&quot;四大名著&quot;</span>)),</span><br><span class="line">                update,</span><br><span class="line">                Book.class);</span><br><span class="line">        System.out.println(updateResult);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 查</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//查询文档</span></span><br><span class="line">    <span class="comment">//db.book.find()</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBook</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; bookList = mongoTemplate.findAll(Book.class);</span><br><span class="line">        System.out.println(bookList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//通过id查询文档</span></span><br><span class="line">    <span class="comment">//db.book.find(&#123;&quot;_id&quot;: ObjectId(&quot;634f8fad38853769fc726cb2&quot;)&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookById</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> mongoTemplate.findById(<span class="string">&quot;6350fb6955ddc14cd4c2b572&quot;</span>, Book.class);</span><br><span class="line">        System.out.println(book);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//条件查询</span></span><br><span class="line">    <span class="comment">//db.book.find(&#123;&quot;score&quot;: &#123;$gt: 8&#125;, &quot;label&quot;:&quot;科幻&quot;&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByCondition</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Book&gt; bookList = mongoTemplate.find(</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Query</span>(Criteria.where(<span class="string">&quot;score&quot;</span>).gt(<span class="number">8</span>).and(<span class="string">&quot;label&quot;</span>).is(<span class="string">&quot;科幻&quot;</span>)), Book.class);</span><br><span class="line">        System.out.println(bookList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//模糊查询</span></span><br><span class="line">    <span class="comment">//db.book.find(&#123;&quot;name&quot;: /^三.*/&#125;)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookByLikeName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//&#x27;三&amp;&#x27;</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">regex</span> <span class="operator">=</span> String.format(<span class="string">&quot;%s%s%s&quot;</span>, <span class="string">&quot;^&quot;</span>, <span class="string">&quot;三&quot;</span>, <span class="string">&quot;.*$&quot;</span>);</span><br><span class="line">        <span class="type">Pattern</span> <span class="variable">pattern</span> <span class="operator">=</span> Pattern.compile(regex, Pattern.CASE_INSENSITIVE);</span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; bookList = mongoTemplate.find(</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Query</span>(Criteria.where(<span class="string">&quot;name&quot;</span>).regex(pattern)), Book.class);</span><br><span class="line">        System.out.println(bookList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 分页查询</span></span><br><span class="line">    <span class="comment">// db.book.find(&#123;name: &#123;$regex: &quot;^三.*$&quot;&#125;&#125;).skip(0).limit(2)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findBookPage</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">pageNum</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">        <span class="type">int</span> <span class="variable">pageSize</span> <span class="operator">=</span> <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//&#x27;三&amp;&#x27;</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">regex</span> <span class="operator">=</span> String.format(<span class="string">&quot;%s%s%s&quot;</span>, <span class="string">&quot;^&quot;</span>, <span class="string">&quot;三&quot;</span>, <span class="string">&quot;.*$&quot;</span>);</span><br><span class="line">        <span class="type">Pattern</span> <span class="variable">pattern</span> <span class="operator">=</span> Pattern.compile(regex, Pattern.CASE_INSENSITIVE);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//条件</span></span><br><span class="line">        <span class="type">Query</span> <span class="variable">query</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Query</span>();</span><br><span class="line"><span class="comment">//        query.addCriteria(Criteria.where(&quot;name&quot;).regex(pattern));</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 统计记录数</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">count</span> <span class="operator">=</span> mongoTemplate.count(query, Book.class);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 分页查询</span></span><br><span class="line">        List&lt;Book&gt; bookList = mongoTemplate.find(query.skip((pageNum - <span class="number">1</span>) * pageSize).limit(pageSize), Book.class);</span><br><span class="line"></span><br><span class="line">        System.out.println(bookList);</span><br><span class="line">        System.out.println(count);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === === === === === 其他查询</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// where name like &#x27;%三%&#x27; and (score &gt; 9 or score &lt; 8)</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findByNameAndScore</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">regex</span> <span class="operator">=</span> String.format(<span class="string">&quot;%s%s%s&quot;</span>, <span class="string">&quot;^.*&quot;</span>, <span class="string">&quot;三&quot;</span>, <span class="string">&quot;.*$&quot;</span>);</span><br><span class="line">        <span class="type">Pattern</span> <span class="variable">pattern</span> <span class="operator">=</span> Pattern.compile(regex, Pattern.CASE_INSENSITIVE);</span><br><span class="line"></span><br><span class="line">        <span class="type">Query</span> <span class="variable">query</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Query</span>();</span><br><span class="line">        query.addCriteria(Criteria.where(<span class="string">&quot;name&quot;</span>).regex(pattern)</span><br><span class="line">                .orOperator(Criteria.where(<span class="string">&quot;score&quot;</span>).gt(<span class="number">9</span>), Criteria.where(<span class="string">&quot;score&quot;</span>).lt(<span class="number">8</span>))</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        System.out.println(query);</span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; books = mongoTemplate.find(query, Book.class);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// where name not like &#x27;&#x27; order by score desc</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">findByNameNotAndOrderByScore</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">regex</span> <span class="operator">=</span> String.format(<span class="string">&quot;%s%s%s&quot;</span>, <span class="string">&quot;^.*&quot;</span>, <span class="string">&quot;三&quot;</span>, <span class="string">&quot;.*$&quot;</span>);</span><br><span class="line">        <span class="type">Pattern</span> <span class="variable">pattern</span> <span class="operator">=</span> Pattern.compile(regex, Pattern.CASE_INSENSITIVE);</span><br><span class="line"></span><br><span class="line">        <span class="type">Query</span> <span class="variable">query</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Query</span>();</span><br><span class="line">        query.addCriteria(Criteria.where(<span class="string">&quot;name&quot;</span>).not().regex(pattern));</span><br><span class="line">        query.with(Sort.by(<span class="string">&quot;score&quot;</span>).descending());</span><br><span class="line"></span><br><span class="line">        System.out.println(query);</span><br><span class="line"></span><br><span class="line">        List&lt;Book&gt; books = mongoTemplate.find(query, Book.class);</span><br><span class="line">        System.out.println(books);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    db.book.aggregate([</span></span><br><span class="line"><span class="comment">//        &#123;$group: &#123;_id: &quot;$label&quot;, count: &#123;$sum: 1&#125;&#125;&#125;,</span></span><br><span class="line"><span class="comment">//        &#123;$match: &#123;count: &#123;$gt: 2&#125;&#125;&#125;</span></span><br><span class="line"><span class="comment">//    ])</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">aggregateOpt</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">        List&lt;AggregationOperation&gt; operations = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        operations.add(Aggregation.group(<span class="string">&quot;label&quot;</span>).count().as(<span class="string">&quot;cnt&quot;</span>));</span><br><span class="line">        operations.add(Aggregation.match(Criteria.where(<span class="string">&quot;cnt&quot;</span>).gt(<span class="number">2</span>)));</span><br><span class="line"></span><br><span class="line">        AggregationResults&lt;Result&gt; results = mongoTemplate.aggregate(Aggregation.newAggregation(operations), <span class="string">&quot;book&quot;</span>, Result.class);</span><br><span class="line">        <span class="keyword">for</span> (Result mappedResult : results.getMappedResults()) &#123;</span><br><span class="line">            System.out.println(mappedResult);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Result</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String _id;</span><br><span class="line">    <span class="keyword">private</span> Integer cnt;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">get_id</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> _id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">set_id</span><span class="params">(String _id)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>._id = _id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getCnt</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> cnt;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setCnt</span><span class="params">(Integer cnt)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.cnt = cnt;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Result&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;label=&#x27;&quot;</span> + _id + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, cnt=&quot;</span> + cnt +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="SpringBoot-整合-MongoDB：GridFSTemplate"><a href="#SpringBoot-整合-MongoDB：GridFSTemplate" class="headerlink" title="SpringBoot 整合 MongoDB：GridFSTemplate"></a>SpringBoot 整合 MongoDB：GridFSTemplate</h1><h2 id="GridFS-概述"><a href="#GridFS-概述" class="headerlink" title="GridFS 概述"></a>GridFS 概述</h2><p>GridFS 是用于存储和检索超过 BSON 文档大小限制（16M）的文件规范。</p>
<p>GridFS 不是将文件存储在单个文档中，而是将文件分成多个部分或块存储，并将每个块存储为单独的文档。在默认情况下，GridFS 使用的块大小为255KB；也就是说，GridFS 将文件分成255KB的块，最后一个块除外。</p>
<blockquote>
<p>GridFS 不支持多文档事务</p>
<p>此处使用到的”chunk”的概念，与分片中的”chunk”的概念并不是同一概念</p>
</blockquote>
<p>GridFS 使用两个集合来存储文件。一个集合存储文件块，另一个存储文件元数据。当向 GridFS 查询文件时，驱动程序就会根据需要重新将块进行组装为文件。我们可以对存储的文件执行范围查询，还可以从文件的任意部分访问其信息。</p>
<h2 id="GridFS-集合"><a href="#GridFS-集合" class="headerlink" title="GridFS 集合"></a>GridFS 集合</h2><p>GridFS 将文件存储在两个集合中：</p>
<ul>
<li>chunks：存储文件的二进制块</li>
<li>files：存储文件的元数据</li>
</ul>
<p>默认情况下，GridFS 会使用名为 “fs” 的 bucket，和两个集合（chunks, files）存储文件：</p>
<ul>
<li>fs.chunks</li>
<li>fs.files</li>
</ul>
<h3 id="chunks-集合"><a href="#chunks-集合" class="headerlink" title="chunks 集合"></a>chunks 集合</h3><p>在 chunks 集合中的每个文档，在 GridFS 中表示文件的不同块，chunks 集合文档的字段如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span> &lt;ObjectId&gt;<span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;files_id&quot;</span><span class="punctuation">:</span> &lt;ObjectId&gt;<span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;n&quot;</span><span class="punctuation">:</span> &lt;num&gt;<span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;data&quot;</span><span class="punctuation">:</span> &lt;binary&gt;</span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p>字段说明：</p>
<ul>
<li>_id：表示当前 chunk 的唯一索引</li>
<li>files_id：表示文件的唯一索引，与 files 集合的 _id 相对应</li>
<li>n：表示 chunk 的编号，GridFS 会对文件的每个块进行编号，从0开始</li>
<li>data：当前 chunk 的数据，BSON 的二进制类型</li>
</ul>
<h3 id="files-集合"><a href="#files-集合" class="headerlink" title="files 集合"></a>files 集合</h3><p>在 files 集合中的每个文档，表示 Grids 中的一个文件，files 集合中的字段如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;_id&quot;</span> : &lt;ObjectId&gt;,</span><br><span class="line">  <span class="string">&quot;length&quot;</span> : &lt;num&gt;,</span><br><span class="line">  <span class="string">&quot;chunkSize&quot;</span> : &lt;num&gt;,</span><br><span class="line">  <span class="string">&quot;uploadDate&quot;</span> : &lt;timestamp&gt;,</span><br><span class="line">  <span class="string">&quot;md5&quot;</span> : &lt;<span class="built_in">hash</span>&gt;,</span><br><span class="line">  <span class="string">&quot;filename&quot;</span> : &lt;string&gt;,</span><br><span class="line">  <span class="string">&quot;contentType&quot;</span> : &lt;string&gt;,</span><br><span class="line">  <span class="string">&quot;aliases&quot;</span> : &lt;string array&gt;,</span><br><span class="line">  <span class="string">&quot;metadata&quot;</span> : &lt;any&gt;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="上传与下载文件"><a href="#上传与下载文件" class="headerlink" title="上传与下载文件"></a>上传与下载文件</h2><ol>
<li>修改配置文件 application.yml</li>
</ol>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8080</span></span><br><span class="line"></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">application:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">mongotemplate</span></span><br><span class="line">  <span class="attr">data:</span></span><br><span class="line">    <span class="attr">mongodb:</span></span><br><span class="line">      <span class="comment">#本地</span></span><br><span class="line">      <span class="attr">uri:</span> <span class="string">mongodb://127.0.0.1:27017</span></span><br><span class="line">      <span class="attr">database:</span> <span class="string">test</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>创建配置类 MongoConfig</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.gridfstemplate.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.MongoClient;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.MongoDatabase;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.gridfs.GridFSBucket;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.gridfs.GridFSBuckets;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MongoConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(&quot;$&#123;spring.data.mongodb.database&#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String db;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> GridFSBucket <span class="title function_">getGridFSBucket</span><span class="params">(MongoClient mongoClient)</span> &#123;</span><br><span class="line">        <span class="type">MongoDatabase</span> <span class="variable">database</span> <span class="operator">=</span> mongoClient.getDatabase(db);</span><br><span class="line">        <span class="type">GridFSBucket</span> <span class="variable">gridFSBucket</span> <span class="operator">=</span> GridFSBuckets.create(database);</span><br><span class="line">        <span class="keyword">return</span> gridFSBucket;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="3">
<li>测试上传与下载文件</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.gridfstemplate;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.gridfs.GridFSBucket;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.gridfs.GridFSDownloadStream;</span><br><span class="line"><span class="keyword">import</span> com.mongodb.client.gridfs.model.GridFSFile;</span><br><span class="line"><span class="keyword">import</span> org.apache.commons.io.IOUtils;</span><br><span class="line"><span class="keyword">import</span> org.bson.types.ObjectId;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.query.Criteria;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.core.query.Query;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.gridfs.GridFsResource;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.mongodb.gridfs.GridFsTemplate;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">GridfstemplateApplicationTests</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> GridFsTemplate gridFsTemplate;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> GridFSBucket gridFSBucket;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 保存文件</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">write</span><span class="params">()</span> <span class="keyword">throws</span> FileNotFoundException &#123;</span><br><span class="line">        <span class="comment">// 获取文件</span></span><br><span class="line"><span class="comment">//        File file = new File(this.getClass().getResource(&quot;/files/a.txt&quot;).getFile());</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="built_in">this</span>.getClass().getResource(<span class="string">&quot;/files/b.pptx&quot;</span>).getFile());</span><br><span class="line">        <span class="comment">// 创建输入流</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fileInputStream</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(file);</span><br><span class="line">        <span class="comment">// 存储文件</span></span><br><span class="line"><span class="comment">//        ObjectId objectId = gridFsTemplate.store(fileInputStream, &quot;a.txt&quot;);</span></span><br><span class="line">        <span class="type">ObjectId</span> <span class="variable">objectId</span> <span class="operator">=</span> gridFsTemplate.store(fileInputStream, <span class="string">&quot;b.pptx&quot;</span>);</span><br><span class="line">        <span class="comment">// 输出返回objectId</span></span><br><span class="line">        System.out.println(objectId);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取文件</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">read</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 查询文件</span></span><br><span class="line">        <span class="type">GridFSFile</span> <span class="variable">gridFSFile</span> <span class="operator">=</span> gridFsTemplate.findOne(Query.query(Criteria.where(<span class="string">&quot;_id&quot;</span>).is(<span class="string">&quot;636de73eb640c4151dbc9193&quot;</span>)));</span><br><span class="line">        <span class="comment">// 打开一个下载流对象</span></span><br><span class="line">        <span class="type">GridFSDownloadStream</span> <span class="variable">gridFSDownloadStream</span> <span class="operator">=</span> gridFSBucket.openDownloadStream(gridFSFile.getObjectId());</span><br><span class="line">        <span class="comment">// 创建GridFsResource对象，获取流</span></span><br><span class="line">        <span class="type">GridFsResource</span> <span class="variable">gridFsResource</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GridFsResource</span>(gridFSFile,gridFSDownloadStream);</span><br><span class="line">        <span class="comment">// 获取对应文件对象</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;C:\\Users\\Lenovo\\Desktop\\download.txt&quot;</span>);</span><br><span class="line">        <span class="comment">// 创建输出流</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fileOutputStream</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(file);</span><br><span class="line">        <span class="comment">// 输出</span></span><br><span class="line">        IOUtils.copy(gridFsResource.getInputStream(),fileOutputStream);</span><br><span class="line"></span><br><span class="line">        fileOutputStream.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 删除指定数据</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">()</span> &#123;</span><br><span class="line">        gridFsTemplate.delete(Query.query(Criteria.where(<span class="string">&quot;_is&quot;</span>).is(<span class="string">&quot;636de65026f9894168fee179&quot;</span>)));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注：具体文件可以自己创建</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/09/18/25%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_03SpringBoot%E6%95%B4%E5%90%88MongoDB/">https://oct25-x.atomgit.net/oct25-xxxxx/2023/09/18/25%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_03SpringBoot%E6%95%B4%E5%90%88MongoDB/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/MongoDB/">MongoDB</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt25.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2023/09/19/26%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_04%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%9B%B8%E5%85%B3/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt26.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">文档数据库 MongoDB_04性能优化相关</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2023/09/09/24%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_02%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt24.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">文档数据库 MongoDB_02集群部署</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2023/09/02/23%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_01%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" title="文档数据库 MongoDB_01概述与基本使用"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt23.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-09-02</div><div class="title">文档数据库 MongoDB_01概述与基本使用</div></div></a></div><div><a href="/oct25-xxxxx/2023/09/09/24%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_02%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2/" title="文档数据库 MongoDB_02集群部署"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt24.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-09-09</div><div class="title">文档数据库 MongoDB_02集群部署</div></div></a></div><div><a href="/oct25-xxxxx/2023/09/19/26%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93%20MongoDB_04%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%9B%B8%E5%85%B3/" title="文档数据库 MongoDB_04性能优化相关"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt26.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-09-19</div><div class="title">文档数据库 MongoDB_04性能优化相关</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#SpringBoot-%E6%95%B4%E5%90%88-MongoDB%EF%BC%9A%E5%AF%BC%E5%8C%85%E5%8F%8A%E9%85%8D%E7%BD%AE"><span class="toc-number">1.</span> <span class="toc-text">SpringBoot 整合 MongoDB：导包及配置</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%AF%BC%E5%85%A5%E7%9B%B8%E5%85%B3%E4%BE%9D%E8%B5%96"><span class="toc-number">1.1.</span> <span class="toc-text">一、导入相关依赖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.</span> <span class="toc-text">二、配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%B9%E5%BA%94%E5%AE%9E%E4%BD%93%E7%B1%BB"><span class="toc-number">1.3.</span> <span class="toc-text">三、创建对应实体类</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#SpringBoot-%E6%95%B4%E5%90%88-MongoDB%EF%BC%9AMongoRepository"><span class="toc-number">2.</span> <span class="toc-text">SpringBoot 整合 MongoDB：MongoRepository</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%95%B0%E6%8D%AE%E8%AE%BF%E9%97%AE%E6%8E%A5%E5%8F%A3"><span class="toc-number">2.1.</span> <span class="toc-text">一、数据访问接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB%E6%B5%8B%E8%AF%95-MongoDB-%E5%9F%BA%E6%9C%AC%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="toc-number">2.2.</span> <span class="toc-text">二、测试类测试 MongoDB 基本的增删改查</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#SpringBoot-%E6%95%B4%E5%90%88-MongoDB%EF%BC%9AMongoTemplate"><span class="toc-number">3.</span> <span class="toc-text">SpringBoot 整合 MongoDB：MongoTemplate</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%B5%8B%E8%AF%95%E7%B1%BB%E6%B5%8B%E8%AF%95-MongoDB-%E5%9F%BA%E6%9C%AC%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="toc-number">3.1.</span> <span class="toc-text">一、测试类测试 MongoDB 基本的增删改查</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#SpringBoot-%E6%95%B4%E5%90%88-MongoDB%EF%BC%9AGridFSTemplate"><span class="toc-number">4.</span> <span class="toc-text">SpringBoot 整合 MongoDB：GridFSTemplate</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#GridFS-%E6%A6%82%E8%BF%B0"><span class="toc-number">4.1.</span> <span class="toc-text">GridFS 概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#GridFS-%E9%9B%86%E5%90%88"><span class="toc-number">4.2.</span> <span class="toc-text">GridFS 集合</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#chunks-%E9%9B%86%E5%90%88"><span class="toc-number">4.2.1.</span> <span class="toc-text">chunks 集合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#files-%E9%9B%86%E5%90%88"><span class="toc-number">4.2.2.</span> <span class="toc-text">files 集合</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E4%B8%8E%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6"><span class="toc-number">4.3.</span> <span class="toc-text">上传与下载文件</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg24.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>